package com.java.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.common.model.Result;
import com.java.common.util.JwtUtil;
import com.java.manage.dao.AccessoriesTypeMapper;
import com.java.manage.dao.ColorMapper;
import com.java.manage.model.AccessoriesType;
import com.java.manage.model.Brand;
import com.java.manage.model.Color;
import com.java.manage.service.AccessoriesTypeService;
import com.java.manage.service.ColorService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccessoriesTypeServiceImpl implements AccessoriesTypeService {
    @Autowired
    private AccessoriesTypeMapper accessoriesTypeMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public Result save(AccessoriesType accessoriesType) {
        accessoriesTypeMapper.save(accessoriesType);
        return Result.success();
    }
    @Override
    public Result modify(AccessoriesType accessoriesType) {
        accessoriesTypeMapper.modify(accessoriesType);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            accessoriesTypeMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result list(AccessoriesType accessoriesType) {
        PageHelper.startPage(accessoriesType.getPageNum(), accessoriesType.getPageSize());

        String token = request.getHeader("Token");
        Integer storeId = (Integer) JwtUtil.getClaims(token).get("storeId");
        accessoriesType.setStoreId(storeId);

        List<AccessoriesType> list = accessoriesTypeMapper.list(accessoriesType);
        PageInfo<AccessoriesType> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }

    @Override
    public Result get(int id) {
        return null;
    }

    /**
     * 查询配件类别信息
     * @return
     */
    @Override
    public Result listAccessType() {
        List<AccessoriesType> accessoriesTypes = accessoriesTypeMapper.listAccessType();
        return Result.success(accessoriesTypes);
    }
}
